package com.hnhcyy.b2b.receiver.service;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.hnhcyy.k3cloud.client.HlyDdjylsClient;
import com.hnhcyy.k3cloud.pojo.HlyDdjyls;
import com.hnhcyy.k3cloud.pojo.Zhifjg;
import com.rabbitmq.client.Channel;

/**
 * Title: OrderFlowReceiver
* Description:  银联商务通道的支付流水
* @author sheyongjun
* @date 2019年3月2日
 */
//@Component
public class ChinaumsPayFlowReceiver {

	private Logger logger = LoggerFactory.getLogger(getClass());
	//金蝶交易流水
	@Autowired
	private HlyDdjylsClient hlyDdjylsClient;

	/**
	 * 银商扫码支付
	 * @author sheyongjun
	 * @date 2019年3月2日
	 */
	@RabbitListener(queues = "b2b.order.chinaumspay.qr.flow")
	public void chinaumspayQr(Message message, Channel channel) throws Exception {
		String text = new String(message.getBody(),"utf-8");
		System.out.println("b2b.order.alipay.flow:" + text);
		try {
			
			JSONObject jsonObject = JSONObject.parseObject(text);
			HlyDdjyls hlyDdjyls = new HlyDdjyls();
			
			Zhifjg zhifjgObj = new Zhifjg();
			String targetSys = jsonObject.getString("targetSys");
			if("Alipay 2.0".equals(targetSys)) {
				zhifjgObj.setFNumber("JSFS32_SYS");// 支付宝
			}else {
				zhifjgObj.setFNumber("JSFS31_SYS");// 微信
			}
			String shanghname = "银联商务"; // 商户号
			String shanghddh =  getSelfOrderNo(jsonObject.getString("billNo"));
			String shanghddrq = jsonObject.getString("payTime");
			String ddje = jsonObject.getIntValue("totalAmount")/100.0 + "";// 订单的金额
			String zhifje = jsonObject.getIntValue("buyerPayAmount")/100.0 + "" ;// 用户实际支付的金额
			String dingdzt =  jsonObject.getString("billStatus");
			String cplydh =  jsonObject.getString("merOrderId");
			String cpddh = jsonObject.getString("targetOrderId");
			String beizhu =  jsonObject.getString("payDetail");
			String zhongdh =  jsonObject.getString("mid");
			String jylx = "PC-微信";
			
			hlyDdjyls.setFBillNo("").setF_shanghname(shanghname).setF_zhifjg(zhifjgObj)
			.setF_shanghddh(shanghddh).setF_shanghddrq(shanghddrq).setF_ddje(ddje)
			.setF_zhifje(zhifje).setF_cpddh("").setF_jylx(jylx).setF_dingdzt(dingdzt).setF_cpckdh("")
			.setF_cpddh(cpddh).setF_cplydh(cplydh).setF_zhongdh(zhongdh).setF_beizhu(beizhu);
			
			boolean tag = hlyDdjylsClient.add(hlyDdjyls);
			if(tag) {
				channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
			}else {
				//ack返回false，并重新回到队列
				channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("======金蝶交易流水保存出错======");
			//丢弃这条消息（绑定了死信队列，该消息会保存到死信队列）
			channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
		}
	}
	
	/**
	 * 银商微信公众号
	 * @author sheyongjun
	 * @date 2019年3月2日
	 */
	@RabbitListener(queues = "b2b.order.chinaumspay.js.flow")
	public void chinaumspayJs(Message message, Channel channel) throws Exception {
		
	}
	
	private static String getSelfOrderNo(String billNo){
		String orderNo = billNo.substring(4,billNo.length()-4); 
		if(StringUtils.isBlank(orderNo)) {
			orderNo = "0000";
		}
		return orderNo;
	}
	
	/*public static void main(String[] args) throws ParseException {
		String text = "{\"respCode\":\"0000\",\"respDesc\":\"成功!\",\"resTime\":\"2019-03-02 16:00:39\",\"mid\":\"898430150470053\","
				+ "\"tid\":\"70348101\",\"instMid\":\"QRPAYDEFAULT\",\"billNo\":\"3032XSDD10120190300667374\",\"billDate\":\"2019-03-02\","
				+ "\"billStatus\":\"PAID\",\"billDesc\":\"消费\",\"memberId\":\"DWI00011467\","
				+ "\"billQRCode\":\"https://qr.chinaums.com/bills/qrCode.do?id=30321903025830001163413942\","
				+ "\"createTime\":\"2019-03-02 16:00:01\",\"merName\":\"湖南恒昌医药有限公司\",\"memo\":\"胡玉龙诊所\","
				+ "\"notifyId\":\"20337a52-6517-4ddb-8ede-9bb46aa16a71\",\"qrCodeType\":\"BILLPAY\",\"totalAmount\":\"12\","
				+ "\"payTime\":\"2019-03-02 16:00:38\",\"targetOrderId\":\"4200000241201903027808963563\","
				+ "\"merOrderId\":\"3032XSDD101201903006673740\",\"status\":\"TRADE_SUCCESS\",\"payDetail\":\"现金支付0.12元。\","
				+ "\"targetSys\":\"WXPay\",\"paySeqId\":\"06654547723N\",\"buyerId\":\"otdJ_uBxYJf0IhoJmpXSMCbUxCac\",\"buyerUsername\":\"\","
				+ "\"buyerPayAmount\":\"12\",\"invoiceAmount\":\"12\",\"totalPayAmount\":\"12\",\"discountAmount\":\"\","
				+ "\"settleDate\":\"2019-03-02\",\"couponAmount\":\"0\",\"billBizType\":\"bills\",\"Sta\":\"1\","
				+ "\"sign\":\"A62481EC1C87D3908566F58DBF5A50D4\"}";
		JSONObject jsonObject = JSONObject.parseObject(text);
		HlyDdjyls hlyDdjyls = new HlyDdjyls();
		
		Zhifjg zhifjgObj = new Zhifjg();
		String targetSys = jsonObject.getString("targetSys");
		if("Alipay 2.0".equals(targetSys)) {
			zhifjgObj.setFNumber("JSFS32_SYS");// 支付宝
		}else {
			zhifjgObj.setFNumber("JSFS31_SYS");// 微信
		}
		String shanghname = "银联商务"; // 商户号
		String shanghddh =  getSelfOrderNo(jsonObject.getString("billNo"));
		String shanghddrq = jsonObject.getString("payTime");
		String ddje = jsonObject.getIntValue("totalAmount")/100.0 + "";// 订单的金额
		String zhifje = jsonObject.getIntValue("buyerPayAmount")/100.0 + "" ;// 用户实际支付的金额
		String dingdzt =  jsonObject.getString("billStatus");
		String cplydh =  jsonObject.getString("merOrderId");
		String cpddh = jsonObject.getString("targetOrderId");
		String beizhu =  jsonObject.getString("payDetail");
		String zhongdh =  jsonObject.getString("mid");
		String jylx = "PC-微信";
		
		hlyDdjyls.setFBillNo("").setF_shanghname(shanghname).setF_zhifjg(zhifjgObj)
		.setF_shanghddh(shanghddh).setF_shanghddrq(shanghddrq).setF_ddje(ddje)
		.setF_zhifje(zhifje).setF_cpddh("").setF_jylx(jylx).setF_dingdzt(dingdzt).setF_cpckdh("")
		.setF_cpddh(cpddh).setF_cplydh(cplydh).setF_zhongdh(zhongdh).setF_beizhu(beizhu);
	}*/

}
